package cn.itsource.org.controller;

import cn.itsource.basic.util.PageResult;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     *
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        if (shop.getId() != null) {
            shopService.update(shop);
        } else {
            shopService.insert(shop);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        shopService.delete(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        shopService.patchDelete(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.selectById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping("/selectAll")
    public List<Shop> findAll() {
        return shopService.selectAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult queryPage(@RequestBody ShopQuery query) {
        return new AjaxResult().me().setResultObj(shopService.selectByQueryPage(query));
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        return shopService.settlment(shop);
    }

    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        return shopService.auditPass(shopAuditLog);
    }

    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        return shopService.auditReject(shopAuditLog);
    }

    //http://localhost:8080/shop/active/
    @GetMapping("/active/{shopId}")
    public void active(@PathVariable("shopId")Long shopId, HttpServletResponse response) throws IOException {
        shopService.active(shopId);
        response.getWriter().print("<script>\n" +
                "  window.setTimeout(function () {\n" +
                "    location.href = 'http://127.0.0.1:8081/#/login'\n" +
                "  },5000)\n" +
                "</script>");
    }

}
